package com.nbsaas.boot.controller.data.batch;

import com.nbsaas.boot.data.api.apis.QueryTemplateApi;
import com.nbsaas.boot.data.api.domain.request.QueryTemplateRequest;
import com.nbsaas.boot.data.api.domain.response.QueryTemplateResponse;
import com.nbsaas.boot.data.ext.apis.ResultBatchHandle;
import com.nbsaas.boot.data.ext.apis.ResultHandleWork;
import com.nbsaas.boot.data.ext.domain.data.HandleData;
import com.nbsaas.boot.data.ext.handle.ResultDbHandle;
import com.nbsaas.boot.data.utils.FreemarkerUtils;
import com.nbsaas.boot.rest.api.SqlExeApi;
import com.nbsaas.boot.rest.request.ResultHandle;
import com.nbsaas.boot.rest.request.ResultHandleObject;
import com.nbsaas.boot.rest.request.SqlObject;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.MapResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResultBatchHandleImpl implements ResultBatchHandle {

    private Map<String, ResultHandleWork> resultHandleWorks = new HashMap<>();

    private SqlExeApi sqlExeApi;

    private QueryTemplateApi queryTemplateApi;


    @Override
    public void setSqlExeApi(SqlExeApi sqlExeApi) {
        this.sqlExeApi = sqlExeApi;
    }

    @Override
    public void setQueryTemplateApi(QueryTemplateApi queryTemplateApi) {
        this.queryTemplateApi = queryTemplateApi;
    }

    @Override
    public void setHandle(Map<String, ResultHandleWork> handles) {
        this.resultHandleWorks = handles;
    }

    @Override
    public void handleList(ResponseObject<HandleData> obj, ListResponse<MapResponse> result) {
        //处理结果
        HandleData handleData = obj.getData();
        QueryTemplateResponse response = handleData.getResponse();
        Integer resultHandleType = response.getResultHandleType();
        if (resultHandleType == null) {
            resultHandleType = 2;
        }
        if (resultHandleType == 2) {
            return;
        }
        if (resultHandleType == 1) {
            //如果结合需要再处理
            ResultHandle resultHandleObject = response.getResultHandle();
            if (resultHandleObject.getResultHandleObjects() != null) {

                List<MapResponse> list = result.getData();
                if (list != null) {
                    for (MapResponse mapResponse : list) {
                        for (ResultHandleObject handleObject : resultHandleObject.getResultHandleObjects()) {

                            String handleType = handleObject.getHandleType();
                            if (!StringUtils.hasText(handleType)) {
                                continue;
                            }
                            ResultHandleWork handle = resultHandleWorks.get(handleType);
                            if (handle == null) {
                                continue;
                            }
                            if (handle instanceof ResultDbHandle) {
                                ResultDbHandle dbHandle = (ResultDbHandle) handle;
                                dbHandle.setSqlExeApi(sqlExeApi);
                                dbHandle.setQueryTemplateApi(queryTemplateApi);
                                dbHandle.setHandleListApi(this::handleList);
                            }
                            handle.handle(handleObject, obj.getData().getParam(), mapResponse);

                        }
                    }

                }


                //处理合并
                List<ResultHandleObject> handles = resultHandleObject.getResultHandleObjects();
                if (handles != null) {
                    for (ResultHandleObject handle : handles) {
                        if ("merge".equals(handle.getResultType())) {
                            String model = handle.getModel();
                            if (!StringUtils.hasText(model)) {
                                continue;
                            }
                            QueryTemplateRequest req = new QueryTemplateRequest();
                            req.setId(handle.getModel());
                            ResponseObject<QueryTemplateResponse> queryTemplateResponseResponseObject = queryTemplateApi.view(req);
                            if (queryTemplateResponseResponseObject.getCode() != 200) {
                                continue;
                            }
                            Map<String, Object> param = obj.getData().getParam();
                            Map<String, Object> requestMap = new HashMap<>(param);
                            SqlObject sqlObject = new SqlObject();
                            sqlObject.setSql(FreemarkerUtils.process(queryTemplateResponseResponseObject.getData().getContent(), requestMap));

                            ListResponse<MapResponse> res = sqlExeApi.list(sqlObject);
                            if (res.getCode() != 200) {
                                continue;
                            }
                            List<MapResponse> items = res.getData();
                            if (items != null) {
                                if (result.getData() == null) {
                                    result.setData(new ArrayList<>());
                                }
                                result.getData().addAll(items);
                            }

                        }
                    }
                }


            }


        }
    }
}
